<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        var nums = [-2,1,-3,4,-1,2,1,-5,4];
        var maxSubArray = function(nums) {
            var sum = nums[0];
            var max = sum;
            for(var i =1;i<nums.length;i++){
                if(sum<0){
                    sum = nums[i];
                }else{
                    sum+=nums[i]
                }
                max = Math.max(sum,max)
            }
            return max;
        };
        console.log(maxSubArray(nums))
    </script>
</body>
</html>